﻿/******************************************************************************
 * Project: Processor Affinity Manager                                        *
 * File:    Picker.cs                                                         *
 * Author:  Aaron Jones                                                       *
 * Version: 1                                                                 *
 *                                                                            *
 * Description:                                                               *
 *    This file defines a windows form that allows the user to specify a      *
 *    processor affinity via a selectable grid.                               *
 ******************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProcessorAffinityManager
{
    public partial class Picker : Form
    {
        public int ProcessorAffinity = 0;

        public Picker()
        {
            InitializeComponent();
            InitializeCheckedListBox();
        }

        public Picker(int affinity)
        {
            InitializeComponent();
            InitializeCheckedListBox();

            for (int i = 0; i < sizeof(int) * 8; i++)
                if ( (affinity & (1 << i)) == (1 << i) )
                    ProcessorCheckedListBox.SetItemChecked(i, true);
        }

        private void InitializeCheckedListBox()
        {
            for (int proc = 1; proc <= System.Environment.ProcessorCount; proc++)
                ProcessorCheckedListBox.Items.Add("CPU " + proc.ToString());
        }

        private void OnClickCancel(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void OnClickOK(object sender, EventArgs e)
        {
            foreach (int i in ProcessorCheckedListBox.CheckedIndices)
                ProcessorAffinity += (1 << i); // &= doesn't work...why?

            DialogResult = DialogResult.OK;
            Close();
        }

        private void OnClickSelectAll(object sender, EventArgs e)
        {
            for (int i = 0; i < ProcessorCheckedListBox.Items.Count; i++)
                ProcessorCheckedListBox.SetItemChecked(i, true);     
        }

        private void OnClickSelectNone(object sender, EventArgs e)
        {
            for (int i = 0; i < ProcessorCheckedListBox.Items.Count; i++)
                ProcessorCheckedListBox.SetItemChecked(i, false);
        }

        private void OnProcessorCheckedListBoxItemChecked(object sender, ItemCheckEventArgs e)
        {
            // This function is called BEFORE the item is updated, so compare with 1, not 0.
            if (e.NewValue == CheckState.Unchecked && ProcessorCheckedListBox.CheckedItems.Count <= 1)
                OKButton.Enabled = false;
            else
                OKButton.Enabled = true;
        }
    }
}
